<template>
  <div class="announcement-detail">
    <div style="text-align: center;padding-top: 20px;" v-show="formData._id">
      <el-radio-group v-model="editState" size="mini">
        <el-radio-button label="basic">基本信息</el-radio-button>
        <el-radio-button label="ivr">按键导航配置</el-radio-button>
      </el-radio-group>
    </div>
    <el-form class="form-wrap" label-position="top" label-width="80px" :model="formData">
      <div v-show="editState === 'basic'" class="basic-info">
        <el-form-item :label="$t('configLazyLoad.process.nodeName')">
          <el-input v-model.trim="formData.DisplayName"></el-input>
        </el-form-item>
        <el-form-item label="按键超时时间：" prop="Timeout">
          <el-input v-model="formData.Timeout" type="number" min="0">
            <template slot="append">秒</template>
          </el-input>
        </el-form-item>
        <el-form-item label="语音文件：">
          <el-select v-model="formData.Filename" placeholder="请选择" style="width: 100%;" filterable>
            <el-option
              label="请选择"
              value="">
            </el-option>
            <el-option
              v-for="item in ivr_sound.ivr_sound"
              v-if="item.SoundType == 'normal'"
              :label="item.DisplayName"
              :value="item.Filename">
            </el-option>
          </el-select>
        </el-form-item>
      </div>
      <div class="edit-ivr-wrap" v-show="editState === 'ivr'" style="border: 1px solid #E7E7EB;">
        <el-row style="padding: 6px 12px 4px 12px;background-color: #F4F5F9;">
          <el-col :span="4">按键</el-col>
          <el-col :span="18">流转到</el-col>
          <el-col :span="2"><i @click="addIvrItem" class="el-icon-circle-plus" style="color: #409EFF;font-size: 18px;cursor: pointer;"></i></el-col>
        </el-row>
        <div>
          <el-row v-for="(item, index) in formData.IvrKeys" style="padding: 6px 12px 14px 12px;border-bottom: 1px solid #E7E7EB;">
            <el-col :span="22">
              <el-row style="padding-top: 8px;">
                <el-col :span="4">
                  <el-select v-model="item.Key" placeholder="请选择" size="mini" style="width: 100%;">
                    <el-option
                      v-for="one in keyArr"
                      :label="one"
                      :value="one">
                    </el-option>
                  </el-select>
                </el-col>
                <el-col :span="18">
                  <node-select-item :ref="'nodeSelect' + index" :curPbx="curPbx" :ToPeerType="formData.IvrKeys[index].ToPeerType" :ToPeer="formData.IvrKeys[index].ToPeer"></node-select-item>
                </el-col>
              </el-row>
              <el-row style="padding-top: 8px;">
                <el-col :span="22">
                  <el-input size="mini" v-model="item.KeyMemo"></el-input>
                </el-col>
              </el-row>
            </el-col>
            <el-col :span="2"><i @click="delIvrItem(index)" class="el-icon-remove" style="color: #FF6B6B;font-size: 18px;margin-top: 30px;cursor: pointer;"></i></el-col>
          </el-row>
        </div>
      </div>
    </el-form>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import {deepClone, isRepeat} from '@/utils/m7Utils'
  import nodeSelectItem from '../../base/nodeSelectItem.vue'
  export default {
    name: 'mainIvrIvrNodeLink',
    data () {
      return {
        keyArr: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '#', '*'],
        editState: 'basic',
        formData: {}
      }
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {}
        }
      }
    },
    components: {
      nodeSelectItem
    },
    computed: {
      Account () {
        return this.$store.state.session.account.account
      },
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      },
      ivr_sound () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      }
    },
    methods: {
      init () {
        this.editState = 'basic'
        this.formData = deepClone(this.form)
        let flag = false
        if (this.ivr_sound.ivr_sound && this.ivr_sound.ivr_sound.length > 0) {
          this.ivr_sound.ivr_sound.forEach(item => {
            if (this.form.Filename === item.Filename) {
              flag = true
            }
          })
        }
        if (!flag) {
          this.formData.Filename = ''
        } else {
          this.formData.Filename = this.form.Filename
        }
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      saveClick () {
        let data = {}
        data.PBX = this.curPbx
        data.Account = this.Account
        if (this.editState === 'basic') {
          if (!this.formData.DisplayName) {
            this.$message({
              type: 'error',
              message: '节点名称不能为空!'
            })
            return
          }
          if (Number(this.formData.Timeout) > 9999 || Number(this.formData.Timeout) < 1) {
            this.$message({
              type: 'error',
              message: '按键超时时间必须大于1秒小于9999秒!'
            })
            return
          }
//          接口接收的参数
          if (this.formData._id) {
            data._id = this.formData._id
          }
          if (this.formData.Exten) {
            data.Exten = this.formData.Exten
          }
          data.DisplayName = this.formData.DisplayName
          data.Timeout = this.formData.Timeout
          data.Filename = this.formData.Filename
          this.$store.dispatch('updateIvr', data).then(res => {
            if (res && res.success) {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.updateVuexPbx()
            }
          })
        } else {
//            校验按键菜单配置的按键不能重复
          let flagArr = []
          this.formData.IvrKeys.forEach(item => {
            flagArr.push(item.Key)
          })
          let flag = false
          flag = isRepeat(flagArr)
          if (flag) {
            this.$message({
              type: 'error',
              message: '按键重复，请重新配置'
            })
            return
          }
          //            校验按键菜单配置的按键不能重复
          //          接口接收的参数
          let IvrKeys = []
          this.formData.IvrKeys.forEach((item, index) => {
            let ivrItem = {}
            ivrItem.Key = item.Key
            ivrItem.KeyMemo = item.KeyMemo
            ivrItem.ToPeerType = this.$refs['nodeSelect' + index][0].ToPeerTypeThis
            ivrItem.ToPeer = this.$refs['nodeSelect' + index][0].ToPeerThis
            IvrKeys.push(ivrItem)
          })
          data.IvrKeys = IvrKeys
          data._id = this.formData._id
          data.Exten = this.formData.Exten
          this.$store.dispatch('updateIvrKey', data).then(res => {
            if (res && res.success) {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.updateVuexPbx()
            }
          })
        }
      },
      updateVuexPbx () {
        this.$store.dispatch('queryIvr', {Account: this.Account, PBX: this.curPbx}).then(resp => {
          if (resp && resp.success) {
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_ivr', data: resp.list || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      },
      addIvrItem () {
        let obj = {
          Key: '1',
          ToPeerType: '',
          ToPeer: '',
          KeyMemo: ''
        }
        if (this.formData.IvrKeys) {
          this.formData.IvrKeys.push(obj)
        } else {
          let arr = []
          arr.push(obj)
          this.$set(this.formData, 'IvrKeys', arr)
        }
      },
      delIvrItem (index) {
        this.formData.IvrKeys.splice(index, 1)
      }
    },
    watch: {
      form: {
        deep: true,
        handler (nv, ov) {
          this.init()
        }
      }
    },
    beforeMount () {
      this.init()
    }
  }
</script>

<style lang="stylus" scoped>
  .announcement-detail
    position relative
    width 100%
    height 100%
    .form-wrap
      padding 20px
  .edit-ivr-wrap
    max-height calc(100vh - 290px)
    overflow-y scroll
    background-color #fff
  .basic-info
    /*width 300px*/
    /*margin 0 auto*/
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
